package cn.hsa.xxx.placeOrder.service.Impl;


import cn.hsa.xxx.placeOrder.bo.DiscountRulesBO;
import cn.hsa.xxx.api.dto.DiscountRulesDTO;
import cn.hsa.xxx.api.service.DiscountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class DiscountServiceImpl implements DiscountService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DiscountRulesBO discountRulesBO;

    @Override
    public Boolean setDiscount(long time) {
        List<DiscountRulesDTO> discountRulesDTOList = discountRulesBO.getAll();

        for (DiscountRulesDTO discountRulesDTO : discountRulesDTOList) {
            //id：优惠券uuid
            String keyId = discountRulesDTO.getDiscountRulesUuid();

            //map：对应uuid优惠券的属性
            Map<String, Object> discountMap = new HashMap<>();
            discountMap.put(DISCOUNT_TYPE, discountRulesDTO.getDiscountType());
            discountMap.put(ALLOW_OVERLAY_FLAG, discountRulesDTO.getAllowOverlayFlag());
            discountMap.put(INDEXES, discountRulesDTO.getIndexes());
            /*discountMap.put("useCategory", discountRulesDTO.getUseCategory());*/
            discountMap.put(PREFERENTIAL_VALUE, discountRulesDTO.getPreferentialValue());

            //放入redis
            redisTemplate.opsForHash().putAll(keyId, discountMap);
            //设置失效时间
            redisTemplate.expire(keyId, time, TimeUnit.MINUTES);
        }
        return true;
    }

    @Override
    public Object getDiscount(String keyId, String hashKey) {
        return keyId == null ? null : (hashKey == null ? null : redisTemplate.opsForHash().get(keyId,hashKey));
    }

    @Override
    public Boolean setIdOfType(long time) {
        List<DiscountRulesDTO> discountRulesDTOList = discountRulesBO.getAll();

        for (DiscountRulesDTO discountRulesDTO : discountRulesDTOList) {
            //当为满减券的时候
            if(discountRulesDTO.getDiscountType().equals("1")) {
                redisTemplate.opsForSet().add(FULL_DISCOUNT, discountRulesDTO.getDiscountRulesUuid());
                redisTemplate.expire(FULL_DISCOUNT, time, TimeUnit.MINUTES);
                log.warn("当为满减券的时候---------------------------");
                continue;
            }

            //当为不可叠加使用的折扣券时
            if(discountRulesDTO.getAllowOverlayFlag().equals("0")) {
                redisTemplate.opsForSet().add(NO_SUPER_DISCOUNT, discountRulesDTO.getDiscountRulesUuid());
                redisTemplate.expire(NO_SUPER_DISCOUNT, time, TimeUnit.MINUTES);
                log.warn("当为不可叠加使用的折扣券时---------------------------");
                continue;
            }

            //当为可叠加使用的折扣券时
            log.warn("getDiscountType:{},getAllowOverlayFlag:{}",discountRulesDTO.getDiscountType(),discountRulesDTO.getAllowOverlayFlag());
            log.warn("当为可叠加使用的折扣券时---------------------------");
            String useCategory = discountRulesDTO.getUseCategory();
            //存入set key：支持的书籍类型  set：支持该书籍类型的对应的优惠券
            redisTemplate.opsForSet().add(useCategory, discountRulesDTO.getDiscountRulesUuid());
            redisTemplate.expire(useCategory, time, TimeUnit.MINUTES);
        }
        return null;
    }

    @Override
    public Set<String> getIdOfType(String bookTypeName) {
        Set<String> discountUuidSet =  redisTemplate.opsForSet().members(bookTypeName);
        //如果没有找到对应类型的折扣券，返回通用类型的折扣券
        if(discountUuidSet == null) {
            return redisTemplate.opsForSet().members("*");
        }
        return discountUuidSet;
    }

}
